package com.test.action;

import java.util.HashSet;
import java.util.List;


import com.test.base.BaseAction;
import com.test.domain.Priviledge;
import com.test.domain.Role;

import com.opensymphony.xwork2.ActionContext;

public class RoleAction extends BaseAction<Role> {
	private static final long serialVersionUID = 1L;
	private Integer[] priviledgeIds;
	
	//直接从BaseAction中继承过来
	//从容器中获取service实例
/*	protected RoleService roleService;
	
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}*/

	public Integer[] getPriviledgeIds() {
		return priviledgeIds;
	}

	public void setPriviledgeIds(Integer[] priviledgeIds) {
		this.priviledgeIds = priviledgeIds;
	}

	/** 列表 */
	public String list() throws Exception {
		// 准备数据
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		return "list";
	}

	/** 删除 */
	public String delete() throws Exception {
		roleService.deleteRole(model.getId());
		return "toList";
	}

	/** 添加页面 */
	public String addUI() throws Exception {
		return "saveUI";
	}

	/** 添加 */
	public String add() throws Exception {
		// 封装对象
		// Role role = new Role();
		// role.setName(model.getName());
		// role.setDescription(model.getDescription());

		// 保存到数据库
		System.out.println("action中获得的id："+model.getId());
		System.out.println("action中获得的岗位名称："+model.getName());
		System.out.println("action中获得的岗位说明："+model.getDescription());
		roleService.addRole(model);

		return "toList";
	}

	/** 修改页面 */
	public String editUI() throws Exception {
		// 准备要回显的数据
		Role role = roleService.findRoleById(model.getId());
		ActionContext.getContext().getValueStack().push(role);
		return "saveUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		// 1，从数据库中获取要修改的原始对象
		Role role = roleService.findRoleById(model.getId());

		// 2, 设置要修改的属性
		role.setName(model.getName());
		role.setDescription(model.getDescription());

		// 3，更新到数据库
		roleService.updateRole(role);

		return "toList";
	}
	public String setPriviledgeUI() throws Exception{
		if(priviledgeService == null){
			System.out.println("Warning! priviledgeService is null!");
		}
		//准备回显所有权限数据,但是未实现树状结构
//		List<Priviledge> priviledgeList = priviledgeService.findAll();
//		ActionContext.getContext().put("priviledgeList", priviledgeList);
		//为了通过tree_view实现树状结构，这里需要每次查询出顶级角色列表
		List<Priviledge> topPriviledgeList = priviledgeService.findTopList();
		ActionContext.getContext().put("topPriviledgeList", topPriviledgeList);
		//准备某个角色已经具备的权限数据
		Role role = roleService.findRoleById(model.getId());
		ActionContext.getContext().getValueStack().push(role);
		priviledgeIds = new Integer[role.getPriviledges().size()];
		int index=0;
		for(Priviledge priviledge : role.getPriviledges()){
			priviledgeIds[index++]=priviledge.getId();
		}
		return "setPriviledgeUI";
	}
	public String setPriviledge() throws Exception{
		//从数据库中取出要修改其权限的原始角色对象信息
		Role role = roleService.findRoleById(model.getId());
		System.out.println("要设置权限的角色ID："+role.getId()+"角色名称："+role.getName());
		System.out.println("要设置的权限标号为：");
		for(int i=0;i<priviledgeIds.length;i++){
			System.out.println(priviledgeIds[i]);
		}
		//根据用户勾选的多个权限的id，从权限表中查询对应的权限实体
		List<Priviledge> priviledgeList = priviledgeService.findByIds(priviledgeIds);
		for(Priviledge priviledge : priviledgeList){
			System.out.println("选择的权限名称为："+priviledge.getName()+" 链接地址为:"+priviledge.getUrl());
		}
		//修改原来角色对象的权限列表信息
		role.setPriviledges(new HashSet<Priviledge>(priviledgeList));
		//将信息更新到数据库中
		roleService.updateRole(role);
		return "toList";
	}

}
